﻿using System;
using System.Diagnostics;
using System.Net;

namespace AirXBMC
{
    public class XbmcController
    {
        private readonly string _xbmcAddress;
        private const string BaseUrl = "/xbmcCmds/xbmcHttp?command=";
        private const string PlayPauseCmd = "Pause()";
        private const string PlayImageCmd = "ShowPicture({0})";

        public XbmcController(string xbmcAddress)
        {
            _xbmcAddress = xbmcAddress;
        }

        private static void CallUrl(string url)
        {
            try
            {
                WebRequest.Create(url).GetResponse();
            }
            catch (Exception)
            {
                Debug.WriteLine("\nERROR: Unable to connect to XBMC\n");
            }
            //var response = HttpWebRequest.Create(url).GetResponse();
            //var reader = new StreamReader(response.GetResponseStream());
            //var urlText = reader.ReadToEnd();
        }

        public void PlayPause()
        {
            CallUrl(_xbmcAddress + BaseUrl + PlayPauseCmd);
        }

        public void PlayImage(string filename)
        {
            CallUrl(_xbmcAddress + BaseUrl + string.Format(PlayImageCmd, filename));
        }

        public void PlayUrl(string url, double position)
        {
            throw new NotImplementedException();
        }
    }
}